package com.pimee.service.admin.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.pimee.common.util.DateUtil;
import com.pimee.common.util.StringUtils;
import com.pimee.mapper.SysRoleMenuMapper;
import com.pimee.mapper.ext.ExtSysRoleMapper;
import com.pimee.model.SysRole;
import com.pimee.model.SysRoleMenu;
import com.pimee.service.BaseService;
import com.pimee.service.admin.ISysRoleService;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

@Slf4j
@Service
public class SysRoleService extends BaseService<SysRole> implements ISysRoleService {
	@Autowired
	private ExtSysRoleMapper extSysRoleMapper;
	@Autowired
	private SysRoleMenuMapper extSysRoleMenuMapper;

	@Override
	public PageInfo<SysRole> pageRole(Map<String, Object> params) {
		log.info("===> 角色列表分页查询...");
		startPage(params);// 设置分页
		List<SysRole> list = extSysRoleMapper.listRole(params);
		return new PageInfo<>(list);
	}

	@Override
	public Set<String> selectRoleKeys(Long userId) {
		List<SysRole> perms = extSysRoleMapper.selectRolesByUserId(userId);
		Set<String> permsSet = new HashSet<>();
		for (SysRole perm : perms) {
			if (StringUtils.isNotNull(perm)) {
				permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(",")));
			}
		}
		return permsSet;
	}

	/**
	 * 根据用户ID查询角色
	 * 
	 * @param userId
	 *            用户ID
	 * @return 角色列表
	 */
	@Override
	public List<SysRole> selectRolesByUserId(Long userId) {
		List<SysRole> userRoles = extSysRoleMapper.selectRolesByUserId(userId);
		List<SysRole> roles = this.listAll();
		for (SysRole role : roles) {
			for (SysRole userRole : userRoles) {
				if (role.getRoleId().longValue() == userRole.getRoleId().longValue()) {
					role.setFlag(true);
					break;
				}
			}
		}
		return roles;
	}

	@Override
	public void insertRole(SysRole param) {
		Date now = DateUtil.now();
		param.setCreateTime(now);
		param.setUpdateTime(now);
		this.saveNotNull(param);
	}

	@Override
	public void updateRole(SysRole param) {
		Date now = DateUtil.now();
		param.setUpdateTime(now);
		this.updateNotNull(param);
	}

	@Override
	public void updateRoleMenu(Long roleId, List<String> menuIds) {
		// 删除角色菜单
		Example example = new Example(SysRoleMenu.class);
		example.createCriteria().andEqualTo("roleId", roleId);
		extSysRoleMenuMapper.deleteByExample(example);
		for(String menuId : menuIds) {
			if(StringUtils.isBlank(menuId)) continue;
			SysRoleMenu sysRoleMenu = new SysRoleMenu();
			sysRoleMenu.setRoleId(roleId);
			sysRoleMenu.setMenuId(Long.valueOf(menuId));
			sysRoleMenu.setCreateTime(DateUtil.now());
			extSysRoleMenuMapper.insertSelective(sysRoleMenu);
		}
	}

	@Override
	public void deleteRole(Long roleId) {
		// 删除角色菜单
		Example example = new Example(SysRoleMenu.class);
		example.createCriteria().andEqualTo("roleId", roleId);
		extSysRoleMenuMapper.deleteByExample(example);
		
		this.deleteByKey(roleId);
	}
	
}
